package grafica;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * @author Patricia
 * 
 */
public class Prototype extends JFrame {
	/** Buttons */
	private JButton loginButton;
	private JButton logoutButton;
	private JButton sendButton;
	/** Labels */
	private JLabel info;
	/** Textfields */ 
	private JTextField usernameTextField;
	/** TextAreas */
	private JTextArea chatTextArea;
	private JTextArea myMessageTextArea;
	
	private String username;

	/**
	 * GUI's initialization
	 */
	public Prototype() {
		initComponents();
	}

	/**
	 * The methods for the GUI drawing
	 */
	public void initComponents() {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("PROTOTYPE");
		setResizable(false);
		setSize(400, 300);
		createPanel();
		setListeners();
		setVisible(true);
	}
	
	/**
	 * Definition of the layout and creation of the components (panel, buttons, labels, ...)
	 */
	private void createPanel(){
		loginButton = new JButton("LOGIN");
		logoutButton = new JButton("LOGOUT");
		sendButton = new JButton("SEND");
		
		info = new JLabel();
		usernameTextField = new JTextField();
		chatTextArea = new JTextArea();
		chatTextArea.setEditable(false);
		myMessageTextArea = new JTextArea();		
		
		
		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(3,1));
		p1.add(usernameTextField);
		p1.add(loginButton);
		p1.add(info);
		
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(3,1));
		p2.add(sendButton);
		p2.add(myMessageTextArea);
		p2.add(logoutButton);
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,1));
		panel.add(p1);
		panel.add(chatTextArea);
		panel.add(p2);
		
		getContentPane().add(panel);
	}
	
	
	/**
	 * Events for the buttons listeners
	 */
	private void setListeners() {
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = myMessageTextArea.getText();
				if ((text != null) && (!text.equals(""))) {
					myMessageTextArea.setText("");
					sendMessage(text);
				}
			}
		});
		
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = usernameTextField.getText();
				if ((text != null) && (!text.equals(""))) {
					login(text);
				}
			}
		});
		
		logoutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				logout();
			}
		});
	}

	/**
	 * Sends user's message to the server and if delivered, the same message will appear in the BirdSong message area 
	 * @param text the string text corresponds the message typed by the user in the message textarea  
	 */
	private void sendMessage(String text){
		//sending message to the server 
		chatTextArea.append(username + "> " + text + "\n");
	}
	
	/**
	 * Method for login the username in the server 
	 * @param username nickname
	 */
	private void login(String username){
		//Connect to the server 
		
		//if succeed
		this.username = username;
		info.setText("LOGGED IN");
	}
	
	/**
	 * Method for disconnect in the server.   
	 */
	private void logout(){
		//disconnect in the server 
		
		//if succeed
		username = "";
		info.setText("LOGGED OUT");
	}
	
	public static void main(String[] args) {
		new Prototype();
	}

}
